#include <iostream>
#include <stdexcept>
#include <initializer_list>
#include <algorithm>
#include <memory>

template <typename T>
class MyVector {
private:
    T* data_;          // 指向数据的指针
    size_t size_;      // 当前元素个数
    size_t capacity_;  // 当前分配的内存空间大小

    // 重新分配内存
    void reallocate(size_t new_capacity) {
        T* new_data = new T[new_capacity];
        
        // 复制现有元素到新内存
        for (size_t i = 0; i < size_; ++i) {
            new_data[i] = std::move(data_[i]);
        }
        
        // 释放旧内存
        delete[] data_;
        
        // 更新数据指针和容量
        data_ = new_data;
        capacity_ = new_capacity;
    }

public:
    // 迭代器类
    class iterator {
    private:
        T* ptr_;
    
    public:
        // 迭代器类型定义
        using iterator_category = std::random_access_iterator_tag;
        using value_type = T;
        using difference_type = std::ptrdiff_t;
        using pointer = T*;
        using reference = T&;
        
        // 构造函数
        iterator(T* ptr = nullptr) : ptr_(ptr) {}
        
        // 解引用操作符
        T& operator*() const {
            return *ptr_;
        }
        
        T* operator->() const {
            return ptr_;
        }
        
        // 前缀递增
        iterator& operator++() {
            ++ptr_;
            return *this;
        }
        
        // 后缀递增
        iterator operator++(int) {
            iterator temp = *this;
            ++(*this);
            return temp;
        }
        
        // 前缀递减
        iterator& operator--() {
            --ptr_;
            return *this;
        }
        
        // 后缀递减
        iterator operator--(int) {
            iterator temp = *this;
            --(*this);
            return temp;
        }
        
        // 加法操作
        iterator operator+(difference_type n) const {
            return iterator(ptr_ + n);
        }
        
        // 减法操作
        iterator operator-(difference_type n) const {
            return iterator(ptr_ - n);
        }
        
        // 两个迭代器之间的距离
        difference_type operator-(const iterator& other) const {
            return ptr_ - other.ptr_;
        }
        
        // 复合赋值操作符
        iterator& operator+=(difference_type n) {
            ptr_ += n;
            return *this;
        }
        
        iterator& operator-=(difference_type n) {
            ptr_ -= n;
            return *this;
        }
        
        // 下标操作符
        T& operator[](difference_type n) const {
            return *(ptr_ + n);
        }
        
        // 比较操作符
        bool operator==(const iterator& other) const {
            return ptr_ == other.ptr_;
        }
        
        bool operator!=(const iterator& other) const {
            return ptr_ != other.ptr_;
        }
        
        bool operator<(const iterator& other) const {
            return ptr_ < other.ptr_;
        }
        
        bool operator>(const iterator& other) const {
            return ptr_ > other.ptr_;
        }
        
        bool operator<=(const iterator& other) const {
            return ptr_ <= other.ptr_;
        }
        
        bool operator>=(const iterator& other) const {
            return ptr_ >= other.ptr_;
        }
    };
    
    // 常量迭代器类型
    using const_iterator = const iterator;
    
    // 默认构造函数
    MyVector() : data_(nullptr), size_(0), capacity_(0) {}
    
    // 带初始容量的构造函数
    explicit MyVector(size_t capacity) : size_(0), capacity_(capacity) {
        data_ = new T[capacity];
    }
    
    // 带初始大小和值的构造函数
    MyVector(size_t size, const T& value) : size_(size), capacity_(size) {
        data_ = new T[size];
        for (size_t i = 0; i < size; ++i) {
            data_[i] = value;
        }
    }
    
    // 初始化列表构造函数
    MyVector(std::initializer_list<T> init) : size_(init.size()), capacity_(init.size()) {
        data_ = new T[capacity_];
        size_t i = 0;
        for (const auto& item : init) {
            data_[i++] = item;
        }
    }
    
    // 复制构造函数
    MyVector(const MyVector& other) : size_(other.size_), capacity_(other.capacity_) {
        data_ = new T[capacity_];
        for (size_t i = 0; i < size_; ++i) {
            data_[i] = other.data_[i];
        }
    }
    
    // 移动构造函数
    MyVector(MyVector&& other) noexcept : data_(other.data_), size_(other.size_), capacity_(other.capacity_) {
        other.data_ = nullptr;
        other.size_ = 0;
        other.capacity_ = 0;
    }
    
    // 析构函数
    ~MyVector() {
        delete[] data_;
    }
    
    // 复制赋值运算符
    MyVector& operator=(const MyVector& other) {
        if (this != &other) {
            delete[] data_;
            size_ = other.size_;
            capacity_ = other.capacity_;
            data_ = new T[capacity_];
            for (size_t i = 0; i < size_; ++i) {
                data_[i] = other.data_[i];
            }
        }
        return *this;
    }
    
    // 移动赋值运算符
    MyVector& operator=(MyVector&& other) noexcept {
        if (this != &other) {
            delete[] data_;
            data_ = other.data_;
            size_ = other.size_;
            capacity_ = other.capacity_;
            other.data_ = nullptr;
            other.size_ = 0;
            other.capacity_ = 0;
        }
        return *this;
    }
    
    // 元素访问
    T& operator[](size_t index) {
        return data_[index];
    }
    
    const T& operator[](size_t index) const {
        return data_[index];
    }
    
    T& at(size_t index) {
        if (index >= size_) {
            throw std::out_of_range("Index out of range");
        }
        return data_[index];
    }
    
    const T& at(size_t index) const {
        if (index >= size_) {
            throw std::out_of_range("Index out of range");
        }
        return data_[index];
    }
    
    T& front() {
        return data_[0];
    }
    
    const T& front() const {
        return data_[0];
    }
    
    T& back() {
        return data_[size_ - 1];
    }
    
    const T& back() const {
        return data_[size_ - 1];
    }
    
    T* data() {
        return data_;
    }
    
    const T* data() const {
        return data_;
    }
    
    // 容量相关
    bool empty() const {
        return size_ == 0;
    }
    
    size_t size() const {
        return size_;
    }
    
    size_t capacity() const {
        return capacity_;
    }
    
    void reserve(size_t new_capacity) {
        if (new_capacity > capacity_) {
            reallocate(new_capacity);
        }
    }
    
    void shrink_to_fit() {
        if (size_ < capacity_) {
            reallocate(size_);
        }
    }
    
    // 修改器
    void clear() {
        size_ = 0;
    }
    
    void push_back(const T& value) {
        if (size_ == capacity_) {
            // 如果没有足够空间，则扩容
            size_t new_capacity = capacity_ == 0 ? 1 : capacity_ * 2;
            reallocate(new_capacity);
        }
        data_[size_++] = value;
    }
    
    void push_back(T&& value) {
        if (size_ == capacity_) {
            // 如果没有足够空间，则扩容
            size_t new_capacity = capacity_ == 0 ? 1 : capacity_ * 2;
            reallocate(new_capacity);
        }
        data_[size_++] = std::move(value);
    }
    
    void pop_back() {
        if (size_ > 0) {
            --size_;
        }
    }
    
    // 在指定位置插入元素
    iterator insert(iterator pos, const T& value) {
        size_t index = pos - begin();
        if (size_ == capacity_) {
            // 如果没有足够空间，则扩容
            size_t new_capacity = capacity_ == 0 ? 1 : capacity_ * 2;
            reallocate(new_capacity);
        }
        
        // 将插入位置之后的元素向后移动
        for (size_t i = size_; i > index; --i) {
            data_[i] = std::move(data_[i - 1]);
        }
        
        data_[index] = value;
        ++size_;
        
        return begin() + index;
    }
    
    // 删除指定位置的元素
    iterator erase(iterator pos) {
        size_t index = pos - begin();
        
        // 将删除位置之后的元素向前移动
        for (size_t i = index; i < size_ - 1; ++i) {
            data_[i] = std::move(data_[i + 1]);
        }
        
        --size_;
        return begin() + index;
    }
    
    // 调整大小
    void resize(size_t new_size) {
        if (new_size > capacity_) {
            reallocate(new_size);
        }
        
        // 如果新大小大于当前大小，用默认值填充新元素
        for (size_t i = size_; i < new_size; ++i) {
            data_[i] = T();
        }
        
        size_ = new_size;
    }
    
    void resize(size_t new_size, const T& value) {
        if (new_size > capacity_) {
            reallocate(new_size);
        }
        
        // 如果新大小大于当前大小，用指定值填充新元素
        for (size_t i = size_; i < new_size; ++i) {
            data_[i] = value;
        }
        
        size_ = new_size;
    }
    
    // 交换两个vector
    void swap(MyVector& other) noexcept {
        std::swap(data_, other.data_);
        std::swap(size_, other.size_);
        std::swap(capacity_, other.capacity_);
    }
    
    // 迭代器相关
    iterator begin() {
        return iterator(data_);
    }
    
    const_iterator begin() const {
        return const_iterator(data_);
    }
    
    const_iterator cbegin() const {
        return const_iterator(data_);
    }
    
    iterator end() {
        return iterator(data_ + size_);
    }
    
    const_iterator end() const {
        return const_iterator(data_ + size_);
    }
    
    const_iterator cend() const {
        return const_iterator(data_ + size_);
    }
};

// 非成员函数
template <typename T>
bool operator==(const MyVector<T>& lhs, const MyVector<T>& rhs) {
    if (lhs.size() != rhs.size()) {
        return false;
    }
    
    for (size_t i = 0; i < lhs.size(); ++i) {
        if (lhs[i] != rhs[i]) {
            return false;
        }
    }
    
    return true;
}

template <typename T>
bool operator!=(const MyVector<T>& lhs, const MyVector<T>& rhs) {
    return !(lhs == rhs);
}

// 使用示例
int main() {
    // 创建一个空的MyVector
    MyVector<int> vec1;
    
    // 使用push_back添加元素
    vec1.push_back(10);
    vec1.push_back(20);
    vec1.push_back(30);
    
    // 使用下标访问元素
    std::cout << "vec1[1] = " << vec1[1] << std::endl;
    
    // 使用迭代器遍历
    std::cout << "vec1 elements: ";
    for (auto it = vec1.begin(); it != vec1.end(); ++it) {
        std::cout << *it << " ";
    }
    std::cout << std::endl;
    
    // 使用初始化列表创建
    MyVector<int> vec2 = {100, 200, 300, 400};
    
    // 使用范围for循环遍历
    std::cout << "vec2 elements: ";
    for (const auto& elem : vec2) {
        std::cout << elem << " ";
    }
    std::cout << std::endl;
    
    // 插入元素
    vec2.insert(vec2.begin() + 2, 250);
    
    // 删除元素
    vec2.erase(vec2.begin());
    
    std::cout << "vec2 after insert and erase: ";
    for (const auto& elem : vec2) {
        std::cout << elem << " ";
    }
    std::cout << std::endl;
    
    // 调整大小
    vec2.resize(7, 999);
    
    std::cout << "vec2 after resize: ";
    for (const auto& elem : vec2) {
        std::cout << elem << " ";
    }
    std::cout << std::endl;
    
    return 0;
}